package com.cnn.test;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.VideoView;


/**
 * Created by jh on 2015/3/13.
 */
public class VideoActivity extends Activity implements View.OnClickListener {

    private VideoView videoView;
    private Button pauseButton,startButton,closeButton;
    private SeekBar seekBar;
    private ImageView img;
    private Uri uri;
    private int sec,minute,hour;
    private TextView currentTime;
    private boolean IsCompletion=false;
    private boolean isStopDrage=false;
    private RelativeLayout video_layout;
    private int progress,duration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.video_view);

        img = (ImageView) findViewById(R.id.img);
        // 加载动画
        Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(
                this, R.anim.update_loading_progressbar_anim);
        img.startAnimation(hyperspaceJumpAnimation);
        pauseButton = (Button) findViewById(R.id.pause);
        pauseButton.setOnClickListener(this);
        seekBar = (SeekBar) findViewById(R.id.progress);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
               if(!isStopDrage){
                   int newPosition=(progress*duration)/100;
                   videoView.seekTo(newPosition);
                   currentTime.setText(generateTime(newPosition));
               }
            }

            @Override
            public void onStartTrackingTouch(SeekBar s) {
                int progress=s.getProgress();
                System.out.println("the seekBar progress is : "+progress);
                isStopDrage=false;
                if(videoView.isPlaying()){
                    videoView.pause();
                }

            }

            @Override
            public void onStopTrackingTouch(SeekBar s) {
                if(startButton.getVisibility()==View.GONE){
                    videoView.start();
                }
                isStopDrage=true;
                videoView.seekTo((s.getProgress()*duration)/100);
            }
        });

        currentTime=(TextView)findViewById(R.id.video_time);
        startButton=(Button)findViewById(R.id.start);
        startButton.setOnClickListener(this);
        closeButton = (Button) findViewById(R.id.close);
        closeButton.setOnClickListener(this);
        //uri = Uri.parse("/mnt/extSdCard/DCIM/Camera/20150717_172620.mp4");
        uri = Uri.parse("http://imc.test.cnnic.cn/static/chinanet.mp4");
        videoView = (VideoView) findViewById(R.id.video_view);
       // videoView.setMediaController(new MediaController(this));
        videoView.setVideoURI(uri);
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                videoView.start();
                videoView.setBackgroundColor(0x00000000);
                duration=videoView.getDuration();
                mHandler.post(r);
                img.clearAnimation();
                img.setVisibility(View.GONE);
            }
        });
        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){

            @Override
            public void onCompletion(MediaPlayer mp) {
                IsCompletion=true;
            }
        });

    }

    Runnable r=new Runnable() {
        @Override
        public void run() {
            if(!IsCompletion){
                mHandler.sendEmptyMessage(1);
                mHandler.postDelayed(r,300);
            }else{
                progress=0;
                IsCompletion=false;
                pauseButton.setVisibility(View.GONE);
                startButton.setVisibility(View.VISIBLE);
                seekBar.setProgress(progress);
                videoView.seekTo(0);
                currentTime.setText(" 0:00:00 ");
            }

        }
    };


    Handler mHandler=new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int temp=videoView.getCurrentPosition();
            currentTime.setText(generateTime(temp));
            progress=(temp*100)/duration;
            seekBar.setProgress(progress);
        }
    };

    public String generateTime(int position){
        position=position/1000;
        hour=position/3600;
        if(hour>0){
            position=position-3600;
        }
        minute=position/60;
        if(minute>0){
            position=position-minute*60;
        }
        sec=position;
        String currentM=null,currentS=null;
        if(minute<10){
            currentM="0"+minute;
        }else{
            currentM=""+minute;
        }
        if(sec<10){
            currentS="0"+sec;
        }else{
            currentS=""+sec;
        }
        return " "+hour+":"+currentM+":"+currentS+" ";
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static int dp2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    @Override
    public void onClick(View v) {

        if (v == pauseButton) {
            pauseButton.setVisibility(View.GONE);
            startButton.setVisibility(View.VISIBLE);
            videoView.pause();
        }
        if (v == startButton) {
            pauseButton.setVisibility(View.VISIBLE);
            startButton.setVisibility(View.GONE);
            videoView.start();
            if(progress==0){
                mHandler.post(r);
            }
        }
        if (v == closeButton) {
            finish();
        }
    }
}
